package org.csu.gp_store.controller;

import org.csu.gp_store.bo.ProductBO;
import org.csu.gp_store.common.CommonResponse;
import org.csu.gp_store.entity.Category;
import org.csu.gp_store.entity.Evaluation;
import org.csu.gp_store.entity.Item;
import org.csu.gp_store.service.CategoryService;
import org.csu.gp_store.service.EvaluationService;
import org.csu.gp_store.service.ItemService;
import org.csu.gp_store.service.ProductService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {


    @Autowired
    private ProductService productService;
    @Autowired
    private ItemService itemService;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private EvaluationService evaluationService;

    //获取所有Category
    @GetMapping("/categories")
    @ResponseBody
    public  CommonResponse<List<Category>> getCategoryList(){
        return CommonResponse.createForSuccess(categoryService.getCategoryList());
    }

    //获取所有商品信息
    @GetMapping("/products")
    @ResponseBody
    public CommonResponse<List<ProductBO>> getProductBOList() {
        return CommonResponse.createForSuccess(productService.getProductBOList());
    }
    //根据CategoryName获取商品信息
    @GetMapping("/products/{categoryID}")
    @ResponseBody
    public CommonResponse<List<ProductBO>> getProductBOListByCategoryID(@PathVariable @NotNull String categoryID) {
        return CommonResponse.createForSuccess(productService.getProductBOListByCategoryID(Long.valueOf(categoryID)));
    }

    //根据ID查询某个商品
    @GetMapping("/products/ID/{productID}")
    @ResponseBody
    public CommonResponse<ProductBO> getProductBOListByProductID(@PathVariable("productID") Long productID) {
        return CommonResponse.createForSuccess(productService.getProductByProductID(productID));
    }

    //根据Name查询某个商品
    @GetMapping("/products/name/{productName}")
    @ResponseBody
    public CommonResponse<List<ProductBO>> getProductBOListByProductName(@PathVariable("productName") String productName) {
        return CommonResponse.createForSuccess(productService.getProductListByProductName(productName));
    }

    //根据关键词KeyWord查询商品

    @GetMapping("/products/keyword/{keyword}")
    @ResponseBody
    public CommonResponse<List<ProductBO>> getProductBOListByKeyword(@PathVariable("keyword") String keyword) {
        return CommonResponse.createForSuccess(productService.getProductListLikeProductName(keyword));
    }


    @PatchMapping("/status/{status}")
    @ResponseBody
    public void updateProductStatus(@PathVariable("status") String status, @RequestParam Long productID) {
        productService.updateProductStatus(productID, status);
        if (Integer.parseInt(status)==0){
            List<Item> itemList =itemService.getItemListByProductID(productID);
            for (Item item :itemList){
                itemService.updateItemStatus(item.getItemID(), Integer.parseInt(status));
            }
        }
    }



    @PatchMapping("/productName/{productName}")
    @ResponseBody
    public void updateProductName(@PathVariable("productName") String productName, @RequestParam Long productID) {
        productService.updateProductName(productID, productName);
    }

    @PatchMapping("/description/{description}")
    @ResponseBody
    public void updateProductDescription(@PathVariable("description") String description, @RequestParam Long productID) {
        productService.updateProductDescription(productID, description);
    }


    @GetMapping("/evaluation/{productID}")
    @ResponseBody
    public CommonResponse<List<Evaluation>> getEvaluationByProductID(@PathVariable Long productID) {
       return CommonResponse.createForSuccess(evaluationService.getEvaluationByProductID(productID));
    }
}
